/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pku.lbs.web;

import cn.edu.pku.ss.lbs.content.Zone;
import cn.edu.pku.ss.lbs.service.LBSService;
import flexjson.JSONSerializer;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import pku.lbs.interfaces.DummyClass;
import pku.lbs.interfaces.Point;

/**
 *
 * @author yongchao
 */
public class SearchWeb {

    static private SearchWeb instance = null;

    private SearchWeb() {}

    static public SearchWeb getInstance() {
        if (instance == null) {
            instance = new SearchWeb();
        }
        return instance;
    }

    public String doSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String posX = request.getParameter("pos_x");
        String posY = request.getParameter("pos_y");

        LBSService lbsService = LBSService.getInstance();
        String zoneId = lbsService.getZone(Integer.valueOf(posX), Integer.valueOf(posY));
        Map<String, String> terminalList = lbsService.getUsers(zoneId);

        JSONObject json = null;
        if (terminalList != null) {
            json = jsonTerminalList( terminalList );
        } else {
            json = new JSONObject();
            json.put("RETURN", true);
        }
        json.put("zone", zoneId);
//        json.put("node", nodeId);

        return json.toString();
    }

    public String doSearchAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        DummyClass dummy = new DummyClass();
//        List<Point> terminalList = dummy.searchAll();

        LBSService lbsService = LBSService.getInstance();
        Map<String, Map<String, Zone>> users = lbsService.getUsers();

        JSONObject json = new JSONObject();
        if (users != null) {
            json = jsonAllUserList( users );
            json.put("RETURN", true);
        } else {
            json.put("RETURN", false);
        }
        return json.toString();
    }

    private JSONObject jsonTerminalList(Map<String, String> users) {
        JSONObject jsonResult = new JSONObject();
        List<JSONObject> result = new ArrayList<JSONObject>();

        for (String userName : users.keySet()) {
            JSONObject jsonItem = new JSONObject();
            String bluetoothAddr = users.get(userName);

            jsonItem.put("userName", userName);
            jsonItem.put("bluetoothAddr", bluetoothAddr);
            result.add( jsonItem );
        }

        JSONSerializer se = new JSONSerializer();
        String resultString = se.serialize( result );
        jsonResult.put("terminalList", resultString);
        jsonResult.put("RETURN", true);

        return jsonResult;
    }

    private JSONObject jsonAllUserList(Map<String, Map<String, Zone>> users) {
        JSONObject jsonResult = new JSONObject();
        List<JSONObject> result = new ArrayList<JSONObject>();

        for (Map<String, Zone> userMap : users.values()) {
            for (String user : userMap.keySet()) {
                Zone zone = userMap.get(user);
                JSONObject jsonItem = new JSONObject();
                int posX = randomInt(zone.getLeft(), zone.getRight());
                int posY = randomInt(zone.getBottom(), zone.getTop());
                jsonItem.put("posX", posX);
                jsonItem.put("posY", posY);
                jsonItem.put("id", user);
                result.add(jsonItem);
            }
        }

        JSONSerializer se = new JSONSerializer();
        String resultString = se.serialize( result );
        jsonResult.put("terminalList", resultString);

        return jsonResult;
    }

    private int randomInt(int from, int to) {
        Random r = new Random();
        return from + r.nextInt(to - from);
    }
}
